{% extends 'base.html' %}
{% load static %}

{% block title %}维修记录管理{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-tools me-2"></i>维修记录管理</h5>
                    <a href="{% url 'assets:maintenance_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus me-1"></i>新建维修记录
                    </a>
                </div>
                
                <div class="card-body">
                    <!-- 搜索和筛选 -->
                    <form method="get" class="row g-3 mb-4">
                        <div class="col-md-3">
                            <input type="text" class="form-control" name="search" 
                                   placeholder="搜索资产名称、编号或标题..." value="{{ search_query }}">
                        </div>
                        <div class="col-md-2">
                            <select class="form-select" name="status">
                                <option value="">全部状态</option>
                                {% for value, label in status_choices %}
                                    <option value="{{ value }}" {% if value == selected_status %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select class="form-select" name="priority">
                                <option value="">全部优先级</option>
                                {% for value, label in priority_choices %}
                                    <option value="{{ value }}" {% if value == selected_priority %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select class="form-select" name="maintenance_type">
                                <option value="">全部类型</option>
                                {% for value, label in type_choices %}
                                    <option value="{{ value }}" {% if value == selected_type %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-3">
                            <button type="submit" class="btn btn-outline-primary">
                                <i class="fas fa-search me-1"></i>搜索
                            </button>
                            <a href="{% url 'assets:maintenance_list' %}" class="btn btn-outline-secondary">
                                <i class="fas fa-undo me-1"></i>重置
                            </a>
                        </div>
                    </form>

                    <!-- 维修记录列表 -->
                    {% if page_obj %}
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead class="table-dark">
                                    <tr>
                                        <th>资产信息</th>
                                        <th>维修标题</th>
                                        <th>类型</th>
                                        <th>状态</th>
                                        <th>优先级</th>
                                        <th>报告人</th>
                                        <th>技师</th>
                                        <th>报告时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for maintenance in page_obj %}
                                        <tr {% if maintenance.is_overdue %}class="table-warning"{% endif %}>
                                            <td>
                                                <strong>{{ maintenance.asset.name }}</strong><br>
                                                <small class="text-muted">{{ maintenance.asset.asset_number }}</small>
                                            </td>
                                            <td>
                                                <strong>{{ maintenance.title }}</strong><br>
                                                <small class="text-muted">{{ maintenance.description|truncatechars:30 }}</small>
                                            </td>
                                            <td>
                                                {% if maintenance.maintenance_type == 'preventive' %}
                                                    <span class="badge bg-info">{{ maintenance.get_maintenance_type_display }}</span>
                                                {% elif maintenance.maintenance_type == 'corrective' %}
                                                    <span class="badge bg-warning">{{ maintenance.get_maintenance_type_display }}</span>
                                                {% elif maintenance.maintenance_type == 'emergency' %}
                                                    <span class="badge bg-danger">{{ maintenance.get_maintenance_type_display }}</span>
                                                {% else %}
                                                    <span class="badge bg-primary">{{ maintenance.get_maintenance_type_display }}</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if maintenance.status == 'pending' %}
                                                    <span class="badge bg-secondary">{{ maintenance.get_status_display }}</span>
                                                {% elif maintenance.status == 'in_progress' %}
                                                    <span class="badge bg-primary">{{ maintenance.get_status_display }}</span>
                                                {% elif maintenance.status == 'completed' %}
                                                    <span class="badge bg-success">{{ maintenance.get_status_display }}</span>
                                                {% else %}
                                                    <span class="badge bg-light text-dark">{{ maintenance.get_status_display }}</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if maintenance.priority == 'urgent' %}
                                                    <span class="badge bg-danger">{{ maintenance.get_priority_display }}</span>
                                                {% elif maintenance.priority == 'high' %}
                                                    <span class="badge bg-warning">{{ maintenance.get_priority_display }}</span>
                                                {% elif maintenance.priority == 'medium' %}
                                                    <span class="badge bg-info">{{ maintenance.get_priority_display }}</span>
                                                {% else %}
                                                    <span class="badge bg-secondary">{{ maintenance.get_priority_display }}</span>
                                                {% endif %}
                                            </td>
                                            <td>{{ maintenance.reporter.get_full_name|default:maintenance.reporter.username }}</td>
                                            <td>{{ maintenance.technician.get_full_name|default:maintenance.technician.username|default:"-" }}</td>
                                            <td>{{ maintenance.reported_at|date:"Y-m-d H:i" }}</td>
                                            <td>
                                                <div class="btn-group btn-group-sm" role="group">
                                                    <a href="{% url 'assets:maintenance_detail' maintenance.pk %}" 
                                                       class="btn btn-outline-primary" title="查看详情">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    {% if user == maintenance.reporter or user.is_staff %}
                                                        <a href="{% url 'assets:maintenance_update' maintenance.pk %}" 
                                                           class="btn btn-outline-secondary" title="编辑">
                                                            <i class="fas fa-edit"></i>
                                                        </a>
                                                    {% endif %}
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- 分页 -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="维修记录分页">
                                <ul class="pagination justify-content-center">
                                    {% if page_obj.has_previous %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page=1{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_priority %}&priority={{ selected_priority }}{% endif %}{% if selected_type %}&maintenance_type={{ selected_type }}{% endif %}">首页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_priority %}&priority={{ selected_priority }}{% endif %}{% if selected_type %}&maintenance_type={{ selected_type }}{% endif %}">上一页</a>
                                        </li>
                                    {% endif %}

                                    <li class="page-item active">
                                        <span class="page-link">第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页</span>
                                    </li>

                                    {% if page_obj.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_priority %}&priority={{ selected_priority }}{% endif %}{% if selected_type %}&maintenance_type={{ selected_type }}{% endif %}">下一页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_priority %}&priority={{ selected_priority }}{% endif %}{% if selected_type %}&maintenance_type={{ selected_type }}{% endif %}">末页</a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                        
                        <!-- 统计信息 -->
                        <div class="row mt-4">
                            <div class="col-md-3">
                                <div class="card bg-light">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-secondary">总记录数</h5>
                                        <h3 class="text-primary">{{ page_obj.paginator.count }}</h3>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="card bg-light">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-secondary">待处理</h5>
                                        <h3 class="text-warning">
                                            {% with pending_count=0 %}
                                                {% for maintenance in page_obj %}
                                                    {% if maintenance.status == 'pending' %}{{ forloop.counter0|add:1 }}{% endif %}
                                                {% empty %}0{% endfor %}
                                            {% endwith %}
                                        </h3>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="card bg-light">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-secondary">维修中</h5>
                                        <h3 class="text-info">
                                            {% with progress_count=0 %}
                                                {% for maintenance in page_obj %}
                                                    {% if maintenance.status == 'in_progress' %}{{ forloop.counter0|add:1 }}{% endif %}
                                                {% empty %}0{% endfor %}
                                            {% endwith %}
                                        </h3>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="card bg-light">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-secondary">已完成</h5>
                                        <h3 class="text-success">
                                            {% with completed_count=0 %}
                                                {% for maintenance in page_obj %}
                                                    {% if maintenance.status == 'completed' %}{{ forloop.counter0|add:1 }}{% endif %}
                                                {% empty %}0{% endfor %}
                                            {% endwith %}
                                        </h3>
                                    </div>
                                </div>
                            </div>
                        </div>
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-tools fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">暂无维修记录</h5>
                            <p class="text-muted">点击上方按钮创建第一个维修记录</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
